<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 今日点名 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <!-- 删选表单 start -->
            <div class="handle-box">
                <!-- 当前老师所属班级 -->
                <el-select v-model="studentVo.student.klassId" placeholder="考勤班级" class="handle-select mr10">
                    <el-option v-for="klass in klassList" :label="klass.name" :key="klass.id" :value="klass.id">
                    </el-option>
                </el-select>
                <!-- 用户 -->
                <el-button type="primary" @click="handleSearch">查询</el-button>
                <el-button type="primary" @click="handleUpdate" icon="el-icon-edit">开始考勤</el-button>
                <el-button type="primary" @click="handleUpdateAttendStatus" icon="el-icon-edit-outline">修改考勤</el-button>
            </div>
            <!-- 删选表单 end -->

            <!-- 表格数据 start -->
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">

                <el-table-column prop="index" label="序号" width="55" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.$index + (studentVo.pageNum - 1) * 10 + 1 }}</span>
                    </template>

                </el-table-column>

                <el-table-column prop="name" label="学生姓名"></el-table-column>
                <el-table-column prop="gender" label="性别"></el-table-column>
                <el-table-column prop="sid" label="学籍号"></el-table-column>

                <el-table-column label="今日考勤状态" align="center">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.attendStatus === 'y'" type="success">正常</el-tag>
                        <el-tag v-if="scope.row.attendStatus === 'l'" type="warning">迟到</el-tag>
                        <el-tag v-if="scope.row.attendStatus === 'n'" type="danger">缺勤</el-tag>
                    </template>
                </el-table-column>

                <el-table-column prop="tel" label="家长电话"></el-table-column>

            </el-table>
            <!-- 表格数据 end -->

            <!-- 分页区 start -->

            <div class="block">
                <el-pagination @current-change="handleCurrentChange" :current-page.sync="studentVo.pageNum" :page-size="3"
                    layout="prev, pager, next, jumper" :page-count="totalPages">
                </el-pagination>
            </div>
            <!-- 分页区 end -->
        </div>
        <!-- 开始考勤弹出框 -->
        <div>
            <el-dialog title="开始考勤" :visible.sync="dialogTableVisible">
                <el-table :data="tableData" border class="table" ref="multipleTable"
                    header-cell-class-name="table-header">
                    <el-table-column prop="index" label="序号" width="55" align="center">
                        <template slot-scope="scope">
                            <span>{{ scope.$index + (studentVo.pageNum - 1) * 10 + 1 }}</span>
                        </template>

                    </el-table-column>
                    <el-table-column prop="name" label="学生姓名"></el-table-column>
                    <el-table-column prop="gender" label="性别"></el-table-column>
                    <el-table-column prop="sid" label="学籍号"></el-table-column>

                    <el-table-column label="今日考勤状态" align="center">
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.attendStatus === 'y'" type="success">正常</el-tag>
                            <el-tag v-if="scope.row.attendStatus === 'l'" type="warning">迟到</el-tag>
                            <el-tag v-if="scope.row.attendStatus === 'n'" type="danger">缺勤</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="tel" label="家长电话"></el-table-column>
                    <el-table-column label="操作" width="180" align="center">
                        <template slot-scope="scope">
                            <el-button type="text" @click="handleStatus(scope.$index, scope.row, 'l')">迟到
                            </el-button>
                            <el-button type="text" class="red" @click="handleStatus(scope.$index, scope.row, 'n')">缺勤
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-dialog>
        </div>
        <!-- 弹出框结束 -->
        <!-- 修改考勤弹出框 -->
        <div>
            <el-dialog title="修改考勤" :visible.sync="TableVisible">
                <el-table :data="tableData" border class="table" ref="multipleTable"
                    header-cell-class-name="table-header">
                    <el-table-column prop="index" label="序号" width="55" align="center">
                        <template slot-scope="scope">
                            <span>{{ scope.$index + (studentVo.pageNum - 1) * 10 + 1 }}</span>
                        </template>

                    </el-table-column>
                    <el-table-column prop="name" label="学生姓名"></el-table-column>
                    <el-table-column prop="gender" label="性别"></el-table-column>
                    <el-table-column prop="sid" label="学籍号"></el-table-column>

                    <el-table-column label="今日考勤状态" align="center">
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.attendStatus === 'y'" type="success">正常</el-tag>
                            <el-tag v-if="scope.row.attendStatus === 'l'" type="warning">迟到</el-tag>
                            <el-tag v-if="scope.row.attendStatus === 'n'" type="danger">缺勤</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="tel" label="家长电话"></el-table-column>
                    <el-table-column label="操作" width="180" align="center">
                        <template slot-scope="scope">
                            <el-button type="text" class="green" @click="handleStatus(scope.$index, scope.row, 'y')">正常
                            </el-button>
                            <el-button type="text" class="blue" @click="handleStatus(scope.$index, scope.row, 'l')">迟到
                            </el-button>
                            <el-button type="text" class="red" @click="handleStatus(scope.$index, scope.row, 'n')">缺勤
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-dialog>
        </div>
        <!-- 弹出框结束 -->
    </div>
</template>

<script>
export default {
    name: 'basetable',
    data() {
        return {
            studentVo: {
                student: {
                    id: '',
                    klassId: '',
                    name: '',
                    gender: '',
                    sid: '',
                    attendStatus: '',
                    tel: '',
                    klass: {
                        id: '',
                        name: ''
                    }
                },
                pageNum: 1
            },
            student: {
                id: '',
                klassId: '',
                name: '',
                gender: '',
                sid: '',
                attendStatus: '',
                tel: '',
                klass: {
                    name: ''
                }
            },
            klassList: [],
            totalPages: 0,
            tableData: [],
            multipleSelection: [],
            delList: [],
            dialogTableVisible: false,
            TableVisible: false,
            editVisible: false,
            viewVisible: false,
            form: {},
            token: '',
            idx: -1,
            id: -1
        };
    },

    created() {
        // this.getData();
        this.getklassList();
        this.getMsg();
        this.token = {
            token: localStorage.getItem('token')
        };
    },

    methods: {
        // 获取当前账号信息tableData
        getMsg() {
            this.$axios.get("/auth/admin/getMe").then((r) => {
                console.log(r);
                this.id = r.data.data.id;
                console.log(this.id);
            });
        },
        // 获取学生列表数据
        getData() {
            this.$axios.post('/attend/admin/list', this.studentVo).then((r) => {
                this.tableData = r.data.data.data;
                // console.log();
                this.totalPages = r.data.data.totalPages;
            });
            // 将考勤数据传入数据库
            this.klassId = this.studentVo.student.klassId;
            console.log(this.klassId);
            this.$axios.post('/attend/admin/addRecord/' + this.klassId).then((r) => {

            });
        },
        // 获取 klassList 数据
        getklassList() {
            this.$axios.get('/attend/admin/getKlassByManagerId').then((r) => {
                this.klassList = r.data.data;
                console.log(r);
            });
        },
        handleSearch() {
            this.getData();
        },
        // 查看详情
        // handleView(index, row) {
        // this.student = row;
        // this.viewVisible = true;
        // this.$axios.get('/goods/getGoodsImages', { params: { id: row.id } }).then((r) => {
        //     console.log(r)
        //     this.goods.goodsImages = r.data.data;
        //     // this.user.userImages = r.data.data;
        // });
        // this.$axios.get('/user/getFans', { params: { id: row.id } }).then((r) => {
        //     this.user.fans = r.data.data;
        // });
        // this.$axios.get('/user/getLikes', { params: { id: row.id } }).then((r) => {
        //     this.user.likes = r.data.data;
        // });
        // },
        handleUpdateAttendStatus() {
            // 打开弹窗
            this.TableVisible = true
            this.klassId = this.studentVo.student.klassId;
            console.log(this.klassId);

        },
        handleUpdate() {
            // 打开弹窗
            this.dialogTableVisible = true
            this.klassId = this.studentVo.student.klassId;
            console.log(this.klassId);
            // 将所有学生的考勤状态设为y
            this.$axios.post('/attend/admin/publish/' + this.klassId).then((r) => {
                console.log(r);
                // 重新调用
                this.getData();
            });
        },
        handleStatus(index, row, attendStatus) {
            console.log(row.attendStatus);
            if (row.attendStatus == attendStatus) {
                if (row.attendStatus == 'n') {
                    this.$message.warning('该同学已被标记为缺勤，切勿重复操作');
                } else if (row.attendStatus == 'l') {
                    this.$message.warning('该同学已被标记为迟到，切勿重复操作');
                } else if (row.attendStatus == 'y') {
                    this.$message.warning('该同学已被标记为正常，切勿重复操作');
                }
                return;
            }
            let confMsg = attendStatus == 'n' ? '确定要将该同学标记为缺勤吗' : attendStatus == 'l' ? '确定要将该同学标记为迟到吗' : '确定要将该同学标记为正常吗';
            this.$confirm(confMsg + '吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$axios.post('/manage/admin/student/update', { id: row.id, attendStatus: attendStatus }).then((r) => {
                        if (r.data.code == 200) {
                            this.$message.success(attendStatus == 'n' ? '标记缺勤成功' : '标记迟到成功');
                            row.attendStatus = attendStatus;
                        } else {
                            this.$message.warning('操作失败，请稍候再试');
                        }
                    });
                })
                .catch(() => { });
        },

        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            this.$message.error(`删除了${str}`);
            this.multipleSelection = [];
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },

        // 分页导航
        handleCurrentChange(pageNum) {
            this.studentVo.pageNum = pageNum;
            this.getData();
            console.log(`当前页: ${pageNum}`);
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.blue {
    color: rgb(247, 115, 1)
}

.green {
    color: rgb(139, 252, 0)
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}

.size-icon {
    font-size: 25px;
}
</style>

